package com.secxiot.provider.communication.controller;

import com.secxiot.provider.CommunicationApi;
import com.secxiot.provider.communication.dto.CommunicationCreateDto;
import com.secxiot.provider.communication.dto.CommunicationDetailCreateDto;
import com.secxiot.provider.communication.service.CommunicationService;
import com.secxiot.provider.communication.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "communications")
@RestController
public class CommunicationController implements CommunicationApi {

    @Autowired
    private CommunicationService communicationService;

    /**
     * 医生查看：病患管理患者列表.
     *
     * @see com.secxiot.provider.CommunicationApi#getPatientList(java.lang.Long,
     * java.lang.String, java.lang.String, java.lang.Long, java.lang.Long)
     */
    @GetMapping("/patientList")
    @Override
    public List<CommunicatePatientVo> getPatientList(Long userId, String userType, String patientName, Long pageNo,
                                                     Long pageSize) {
        return communicationService.getPatientList(userId, userType, patientName, pageNo * pageSize, pageSize);
    }

    /**
     * 患者查看：我的医生列表.
     *
     * @see com.secxiot.provider.CommunicationApi#getDoctorList(java.lang.Long,
     * java.lang.String, java.lang.Long, java.lang.Long)
     */
    @GetMapping("/doctorList")
    @Override
    public List<CommunicateDoctorVo> getDoctorList(Long patientId, String doctorName, Long pageNo, Long pageSize) {
        return communicationService.getDoctorList(patientId, doctorName, pageNo * pageSize, pageSize);
    }

    @PostMapping("/updatePatientExtro")
    @Override
    public boolean updatePatientExtro(Long patientId, String memo, String beds) {
        return communicationService.updateUserExtro(patientId, memo, beds);
    }

    @PostMapping("/updateDoctorExtro")
    @Override
    public boolean updateDoctorExtro(Long doctorId, String memo) {
        return communicationService.updateUserExtro(doctorId, memo, null);
    }

    /**
     * 获取主题列表
     *
     * @param userId    登录用户id
     * @param userType  登录用户类型，字典00001
     * @param title     主题标题
     * @param doctorId  医生/护士id
     * @param patientId 患者id
     * @param pageNo    页码
     * @param pageSize  条数
     * @return
     */
    @GetMapping("/communicationlist")
    @Override
    public List<CommunicationVo> getCommunicationlist(Long userId, String userType, Long doctorId, Long patientId,
                                                      String title, Long pageNo, Long pageSize) {
        return communicationService.getCommunicationlist(userId, userType, title, doctorId, patientId,
                pageNo * pageSize, pageSize);
    }

    /**
     * 主题对话列表.
     */
    @GetMapping("/communicationDetaillist")
    @Override
    public List<CommunicationDetailVo> getCommunicationDetaillist(Long readUserId, Long communicationId, Long pageNo,
                                                                  Long pageSize) {
        return communicationService.getCommunicationDetaillist(readUserId, communicationId, pageNo * pageSize,
                pageSize);
    }

    /**
     * 新增主题.
     *
     * @see com.secxiot.provider.CommunicationApi#createCommunication(com.secxiot.provider.communication.dto.CommunicationCreateDto)
     */
    @PostMapping("/createCommunication")
    @Override
    public Long createCommunication(CommunicationCreateDto communicationCreateDto) {
        return communicationService.createCommunication(communicationCreateDto);
    }

    /**
     * 新增主题对话
     *
     * @see com.secxiot.provider.CommunicationApi#createCommunicationDetail(com.secxiot.provider.communication.dto.CommunicationDetailCreateDto)
     */
    @PostMapping("/createCommunicationDetail")
    @Override
    public Long createCommunicationDetail(CommunicationDetailCreateDto communicationCreateDetailDto) {
        return communicationService.createCommunicationDetail(communicationCreateDetailDto);
    }

    /**
     * 删除主题.
     *
     * @see com.secxiot.provider.CommunicationApi#deleteCommunication(java.lang.Long,
     * java.lang.Long)
     */
    @DeleteMapping("/deleteCommunication")
    @Override
    public boolean deleteCommunication(Long userId, Long communicationId) {
        return communicationService.deleteCommunication(userId, communicationId);
    }

    /**
     * 修改主题状态为已读.
     *
     * @see com.secxiot.provider.CommunicationApi#updateCommunicationReadFlag(java.lang.Long,
     * java.lang.Long, java.lang.Long)
     */
    @PostMapping("/updateCommunicationReadFlag")
    @Override
    public boolean updateCommunicationReadFlag(
            @RequestParam(value = "communicationId", required = true) Long communicationId,
            @RequestParam(value = "communicationDetailId", required = false) Long communicationDetailId,
            @RequestParam(value = "readUserId", required = true) Long readUserId) {
        return communicationService.updateCommunicationReadFlag(communicationId, readUserId);
    }

    @PostMapping("/unread/count")
    @Override
    public CommunicationUnreadCount getUnreadCount(@RequestParam(value = "readUserId", required = true) Long readUserId,
                                                   @RequestParam(value = "userType", required = true) String userType) {
        return communicationService.getUnreadCount(readUserId, userType);
    }

    /**
     * 主题对话列表.
     */
    @GetMapping("/getcommunicationTalklist")
    @Override
    public List<CommunicationTalkVo> getcommunicationTalklist(Long talkUserId, Long userId, Long pageNo, Long pageSize) {
        return communicationService.getcommunicationTalklist(talkUserId, userId, pageNo * pageSize, pageSize);
    }

    @PostMapping("/sendTalkMessage")
    @Override
    public Long sendTalkMessage(Long talkUserId, Long userId, String content, Integer contentType) {
        return communicationService.sendTalkMessage(talkUserId, userId, content, contentType);
    }
}
